#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <stack>
using namespace std;
#include "family_tree.hpp"
#include "file.hpp"

int main()
{
	fstream file1;
	file1.open("/home/abc/桌面/opencv/GenealogicalList.txt", ios::in);
	if (file1.fail()) {
		cout << "error0: Open file failure!"<< endl;
		exit(1);
	}
	FamilyTreeNode* root = new FamilyTreeNode;
	CreateGengalogyFile(root, file1, 0);
	file1.close();
	FamilyTreeNode* parent = NULL;
	SetParent(root, parent);
	cout << "----------------------------欢迎进入家谱管理系统------------------------------" << endl;
	while (1) {
		MenuInterface();
		int choice;
		cout << "请输入选择:";
		cin >> choice;
		cin.ignore();
		switch (choice) {
			case 1: {
				file1.open("/home/abc/桌面/opencv/GenealogicalList.txt", ios::out);
				if (file1.fail()) {
					cout << "error1: Open file failure!" << endl;
					exit(1);
				}
				InsertChild(root);
				SaveFile(root, file1);
				file1.close();
				break;
			}
			case 2: {
				file1.open("/home/abc/桌面/opencv/GenealogicalList.txt", ios::out);
				if (file1.fail()) {
					cout << "error2: Open file failure!" << endl;
					exit(1);
				}
				ModifyMember(root);
				SaveFile(root, file1);
				file1.close();
				break;
			}
			case 3: {
				file1.open("/home/abc/桌面/opencv/GenealogicalList.txt", ios::out);
				if (file1.fail()) {
					cout << "error3: Open file failure!" << endl;
					exit(1);
				}
				DeleteMember(root);
				SaveFile(root, file1);
				file1.close();
				break;
			}
			case 4: {
				PrintAllMessage(root);
				break;
			}
			case 5: {
				cout << "请输入要查询的代数" << endl;
				int depth;
				cin >> depth;
				cin.ignore();
				PrintDepthMessage(root, depth);
				break;
			}
			case 6: {
				PrintMessageByNameAndBirthdate(root);
				break;
			}
			case 7: {
				int count = 0, totalAge = 0;
				countMembers(root, count, totalAge);
				cout << "家谱成员总数为：" << count << endl;
				cout << "平均寿命：" << getAverageAge(root) << endl;
				getGenderRatio(root);
				break;
			}
			case 8: {
				exit(1);
			}
			default: {
				cout << "输入数字非法，请重新输入!" << endl;
				break;
			}
		}
	}
}


